package com.example.common;

/**
 * 统一响应结果类
 * @author System
 * @date 2024
 */
public class Result<T> {
    private Integer code;
    private String message;
    private Boolean success;
    private T data;

    public Result() {}

    public Result(Integer code, Boolean success,String message) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public Result(Integer code,Boolean success, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = success;
    }

    /**
     * 成功响应
     */
    public static <T> Result<T> success() {
        return new Result<>(200, true,"操作成功");
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(200, true,"操作成功", data);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result<>(200,true, message, data);
    }

    /**
     * 失败响应
     */
    public static <T> Result<T> error() {
        return new Result<>(500,false, "操作失败");
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(500,false, message);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code,false, message);
    }

    /**
     * 参数错误
     */
    public static <T> Result<T> badRequest(String message) {
        return new Result<>(400,false, message);
    }

    /**
     * 未授权
     */
    public static <T> Result<T> unauthorized() {
        return new Result<>(401, false,"未授权访问");
    }
    public static <T> Result<T> unauthorized(String message) {
        return new Result<>(401,false, message);
    }

    /**
     * 禁止访问
     */
    public static <T> Result<T> forbidden() {
        return new Result<>(403,false, "禁止访问");
    }
    public static <T> Result<T> forbidden(String message) {
        return new Result<>(403,false, message);
    }

    /**
     * 资源未找到
     */
    public static <T> Result<T> notFound() {
        return new Result<>(404, false,"资源未找到");
    }
    public static <T> Result<T> notFound(String message) {
        return new Result<>(404,false, message);
    }

    // Getter和Setter方法
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", success=" + success + '\''+
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}